﻿using CJGL;
using Comm;
using FastReport;
using FastReport.Export.Dbf;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net.Sockets;
using System.Reflection;
using System.Reflection.Emit;
using System.Runtime.ConstrainedExecution;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Web.UI.WebControls;
using System.Windows.Forms;
using System.Xml.Linq;
using static CJGL.ExternalApiService;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;
using static System.Windows.Forms.VisualStyles.VisualStyleElement.ToolBar;

namespace CJGL
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void 系统配置ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ConfigForm passfrom = new ConfigForm();


            if (passfrom.ShowDialog(this) == DialogResult.OK)
            {

            }
        }
        ExternalApiService ser = new ExternalApiService();
        private void Form1_Load(object sender, EventArgs e)
        {
            //加载配置
            ClsParmart.LoadPar();
            // scanner1.finPrint += Scanner1_finPrint;
            printer.ErrorOccurred += Printer_ErrorOccurred;
            printer.PrintCompleted += Printer_PrintCompleted;

        }
        // 1. 定义参数类
        public class TaskParams
        {
            public int ProductId { get; set; }
            public List<BigCodeMapping> Codes { get; set; }
        }


        // 新增字段
        private List<BigCodeMapping> pendingMappings = new List<BigCodeMapping>();

        private void Printer_PrintCompleted(object sender, InkjetPrinterController.PrintCompletedEventArgs e)
        {
            insetLog("小码喷码完成:" + lastCode);
            string smallCode = lastCode;
            string centerCode = lastcenterCode;
            string boxCode = lastboxCode;
            SqlDAL.SetCodeUsed("code_table_lv1", smallCode);
            smallCodes.Add(smallCode);
            smallCodeCount++;

            // listBox2显示小码和当前大码
            this.Invoke(new Action(() =>
            {
                this.listBox2.Items.Add($"{smallCode} | {centerCode}");
                this.groupBox1.Text = "当前小码(" + smallCodeCount + ")";
            }));

            SqlDAL.ReplenishAllCodes();
            //centerSize = 3;
            // 满一中码数量
            if (smallCodeCount >= centerSize)
            {
                SqlDAL.SetCodeUsed("code_table_lv2", centerCode);
                centerCodes.Add(centerCode);
                centerCodeCount++;
                Task.Run( () =>
                {
                    PrintLabel(centerCode, 1);
                });


                // 构造中码关联小码
                var centerMapping = new CenterCodeMapping
                {
                    centerCode = centerCode,
                    smallCodes = new List<string>(smallCodes)
                };

                // 构造大码关联中码和小码（暂时只一个中码，后续大箱满了再合并）
                var bigMapping = pendingMappings.FirstOrDefault(m => m.bigCode == boxCode);
                if (bigMapping == null)
                {
                    bigMapping = new BigCodeMapping
                    {
                        bigCode = boxCode,
                        product_batch_name = PCH,
                        centerCodes = new List<CenterCodeMapping>()
                    };
                    pendingMappings.Add(bigMapping);
                }
                bigMapping.centerCodes.Add(centerMapping);
                insetLog("关联中码:" + centerCode);
                smallCodes.Clear();
                smallCodeCount = 0;
                this.Invoke(new Action(() =>
                {
                    this.listBox2.Items.Clear();
                    this.listBox3.Items.Add($"{centerCode} | {boxCode}");
                    this.groupBox1.Text = "当前小码(" + smallCodeCount + ")";
                    this.groupBox4.Text = "当前中码(" + centerCodeCount + ")";
                }));

                //继续获取中码打印
                DataTable centerdt = SqlDAL.GetUnusedCode("code_table_lv2");
                if (centerdt.Rows.Count == 0)
                {
                    insetLog("无可用中码");
                    return;
                }
                lastcenterCode = centerdt.Rows[0]["code_str"].ToString();
            }
            //boxSize = 2;
            // 满一大箱数量
            if (centerCodeCount >= boxSize)
            {

                SqlDAL.SetCodeUsed("code_table_lv3", boxCode);

                Task.Run( () =>
                {
                    PrintLabel(boxCode, 2);
                });


                var codesCopy = pendingMappings == null ? new List<BigCodeMapping>() : JsonConvert.DeserializeObject<List<BigCodeMapping>>(JsonConvert.SerializeObject(pendingMappings));
                // 清空内存和UI
                pendingMappings.Clear();
                centerCodes.Clear();
                centerCodeCount = 0;
                bigCount++;
                this.Invoke(new Action(() =>
                {
                    this.listBox3.Items.Clear();
                    this.groupBox1.Text = "当前小码(" + smallCodeCount + ")";
                    this.groupBox4.Text = "当前中码(" + centerCodeCount + ")";
                    this.label6.Text = bigCount.ToString();


                }));


                // 修正为不带参数的 lambda（因为 Task.Run 期望 Action 或 Func<Task>，而不是带参数的 Action<object>）：
                Task.Run(async () =>
                {
                    //insetLog("上传成功" + json);
                    try
                    {
                        // 3. 调用异步方法
                        var result = await ser.BatchRelationLevel3Async(productId, 2, codesCopy);

                        // 4. 处理结果（需同步到UI线程）
                        if (result?.success == true)
                        {
                            insetLog("上传成功：" + codesCopy[0].bigCode);
                        }
                        else
                        {
                            insetLog($"上传失败: {result?.message + " " + codesCopy[0].bigCode}");
                        }

                    }
                    catch (Exception ex)
                    {
                        insetLog("任务异常:" + ex.Message + " 数据内容:");
                    }
                });



                // 上传所有关联数据
                //var ret = await  ser.BatchRelationLevel3Async45(productId, 2, new List<BigCodeMapping>(codesCopy));
                //if (!ret.success)
                //{
                //    insetLog("上传大码关联数据失败:" + ret.message);
                //}
                //else
                //{
                //    insetLog("上传大码关联数据成功");
                //}

                //// 清空内存和UI
                //pendingMappings.Clear();
                //centerCodes.Clear();
                //centerCodeCount = 0;
                //bigCount++;
                //this.Invoke(new Action(() =>
                //{
                //    this.listBox3.Items.Clear();
                //    this.groupBox1.Text = "当前小码(" + smallCodeCount + ")";
                //    this.groupBox4.Text = "当前中码(" + centerCodeCount + ")";
                //    this.label6.Text = bigCount.ToString();
                //}));

                //继续获取中码打印
                DataTable boxdt = SqlDAL.GetUnusedCode("code_table_lv3");
                if (boxdt.Rows.Count == 0)
                {
                    insetLog("无可用小码");
                    return;
                }
                lastboxCode = boxdt.Rows[0]["code_str"].ToString();
            }

            //继续获取小码打印
            DataTable dt = SqlDAL.GetUnusedCode("code_table_lv1");
            if (dt.Rows.Count == 0)
            {
                insetLog("无可用小码");
                return;
            }
            string boxCodeprint = dt.Rows[0]["code_str"].ToString();
            printer.SendPrintData(boxCodeprint);
            lastCode = boxCodeprint;
        }

        private void PrintLabel(string mes1, int boxType)
        {
            lock (o)
            {


                Report report1 = new Report();
                if (boxType == 1)
                {
                    report1.Load(@"print1.frx");
                }
                else
                {
                    report1.Load(@"print2.frx");
                }
                string PrintNumberte = NumberToLetter();
                report1.SetParameterValue("date", mes1);
                report1.SetParameterValue("PrintNumber", PrintNumberte);
                report1.SetParameterValue("proName", proName);
                report1.PrintSettings.ShowDialog = false;
                report1.PrintSettings.Printer = ClsParmart.cboprint1;
                if (boxType != 1)
                {
                    Thread.Sleep(100);
                    report1.Print();
                }
                else
                {
                    report1.Print();
                }
                if (boxType != 1)
                {
                    int number = Convert.ToInt32(ClsParmart.PrintNumber);
                    number = number + 1;
                    ClsParmart.PrintNumber = number.ToString();
                    XML.write("PrintNumber", number.ToString(), "p.xml");
                }
            }

        }

        //private Task PrintLabelAsync(string mes1, int boxType)
        //{
        //    // 使用 Task.Factory.StartNew 替代 Task.Run
        //    return Task.Factory.StartNew(() =>
        //    {
        //        Report report1 = new Report();
        //        string reportPath = boxType == 1 ? @"print1.frx" : @"print2.frx";

        //        // 同步加载报表（假设 Load 是同步方法）
        //        report1.Load(reportPath);

        //        string PrintNumberte = NumberToLetter();

        //        // 设置报表参数
        //        report1.SetParameterValue("date", mes1);
        //        report1.SetParameterValue("PrintNumber", PrintNumberte);
        //        report1.SetParameterValue("proName", proName);

        //        // 配置打印设置
        //        report1.PrintSettings.ShowDialog = false;
        //        report1.PrintSettings.Printer = ClsParmart.cboprint1;

        //        // 同步打印（假设 Print 是同步方法）
        //        report1.Print();

        //        if (boxType != 1)
        //        {
        //            int number = Convert.ToInt32(ClsParmart.PrintNumber);
        //            number++;
        //            ClsParmart.PrintNumber = number.ToString();

        //            // 同步写入文件（用 Task 包装 IO 操作）
        //            XML.write("PrintNumber", number.ToString(), "p.xml");
        //        }
        //    }, CancellationToken.None, TaskCreationOptions.None, TaskScheduler.Default);
        //}

        private void Printer_ErrorOccurred(object sender, InkjetPrinterController.ErrorOccurredEventArgs e)
        {
            insetLog("报错了" + e);
        }

        InkjetPrinterController printer = new InkjetPrinterController();


        private void insetLog(string mes)
        {
            WriteFileCls.InserLog(mes);
            mes = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss：") + mes;

            this.Invoke(new Action(() =>
            {
                this.listBox1.Items.Insert(0, mes);
            }));
        }


        private void cboHospital_SelectedIndexChanged(object sender, EventArgs e)
        {

            if (cboHospital.SelectedIndex != -1 && isInti)
            {
                ClsParmart.SelProId = cboHospital.SelectedValue.ToString();
                ProductItem nowtime = (ProductItem)cboHospital.SelectedItem;
                this.textBox2.Text = nowtime.center_size + ":" + nowtime.box_size;
                label1.Text = nowtime.product_size;
            }
        }

        // 队列和计数
        private List<string> smallCodes = new List<string>();
        private List<string> centerCodes = new List<string>();
        private int smallCodeCount = 0;
        private int centerCodeCount = 0;
        private int boxSize = 0;
        private int centerSize = 0;
        private int bigCount = 0;
        private int productId = 0;
        private string lastCode = "";
        private string lastcenterCode = "";
        private string lastboxCode = "";
        private string proName = "";

        private string PCH = "";

        private void button3_Click(object sender, EventArgs e)
        {

            UIEnabled(false);
            if (button3.Text == "启动")
            {
                if (txtBCH.Text.Trim() == "")
                {
                    UIEnabled(true);
                    insetLog("启动失败，请输入批次号");
                    return;
                }
                else
                {
                    PCH = txtBCH.Text.Trim();
                }


                if (cboHospital.SelectedIndex != -1 && comboBox2.SelectedValue != null)
                {



                    ProductItem nowtime = (ProductItem)cboHospital.SelectedItem;
                    boxSize = nowtime.center_size;
                    centerSize = nowtime.box_size;
                    productId = nowtime.product_id;
                    proName = nowtime.product_name + "(" + this.comboBox2.SelectedValue.ToString() + ")";

                    insetLog("下载数据中...");
                    SqlDAL.ReplenishAllCodes();
                    insetLog("下载数据完成...");

                    bool isok = printer.Connect(ClsParmart.alert_prot, Convert.ToInt32(ClsParmart.Baudrate));
                    if (!isok)
                    {
                        insetLog("喷码机打开失败:" + isok);
                        UIEnabled(true);
                        return;
                    }
                    printer.ClearData();
                    DataTable smallBox = SqlDAL.GetUnusedCode("code_table_lv1");
                    if (smallBox.Rows.Count == 0)
                    {
                        UIEnabled(true);
                        insetLog("无可用小码");
                        return;
                    }
                    DataTable dtCenter = SqlDAL.GetUnusedCode("code_table_lv2");
                    if (dtCenter.Rows.Count == 0)
                    {
                        UIEnabled(true);
                        insetLog("无可用中码");
                        return;
                    }
                    lastcenterCode = dtCenter.Rows[0]["code_str"].ToString();

                    DataTable dtBox = SqlDAL.GetUnusedCode("code_table_lv3");
                    if (dtBox.Rows.Count == 0)
                    {
                        UIEnabled(true);
                        insetLog("无可用大码");
                        return;
                    }
                    lastboxCode = dtBox.Rows[0]["code_str"].ToString();
                    lastCode = smallBox.Rows[0]["code_str"].ToString();


                    string ProductCategory = this.comboBox1.SelectedValue.ToString();

                    string PackCategory = this.comboBox2.SelectedValue.ToString();
                    XML.write("ProductCategory", ProductCategory, "p.xml");
                    XML.write("PackCategory", PackCategory, "p.xml");
                    XML.write("ProId", productId.ToString(), "p.xml");

                    printer.SendPrintData(lastCode);



                    button3.Enabled = true;
                    insetLog("启动软件成功!产品名称：" + nowtime.product_name + "(" + nowtime.product_id + ") 产品类别：" + ProductCategory + " 包装类别：" + PackCategory + " 包装比例:" + nowtime.product_size);
                    button3.Text = "停止";
                }
                else
                {
                    UIEnabled(true);
                    insetLog("启动失败，清选择产品");
                }
            }
            else
            {

                printer.Dispose();
                UIEnabled(true);
                button3.Text = "启动";
                if (centerCodeCount > 0 || smallCodeCount > 0)
                {
                    insetLog("有剩余数据，请先不满箱上传，或者继续生产！！");
                    cboHospital.Enabled = false;
                    comboBox1.Enabled = false;
                    comboBox2.Enabled = false;
                }



            }
        }

        private void UIEnabled(bool isEnabled)
        {
            cboHospital.Enabled = isEnabled;
            comboBox1.Enabled = isEnabled;
            comboBox2.Enabled = isEnabled;
            button1.Enabled = isEnabled;
            button3.Enabled = isEnabled;
            txtBCH.Enabled = isEnabled;
            button2.Enabled = isEnabled;
            button4.Enabled = isEnabled;
            textBox4.Enabled = isEnabled;
            button5.Enabled = isEnabled;
        }



        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            printer.Dispose();
        }

        private void button4_Click(object sender, EventArgs e)
        {
            SqlDAL.ReplenishAllCodes();
        }

        private void 换肤ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            skinEngine1.SkinFile = "MacOS.ssk";
        }

        bool isInti = false;

        private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
            this.Invoke(new Action(() =>
            {

                CategoryResult list = ser.GetProductCategory();
                this.comboBox1.DataSource = list.details;

                CategoryResult list1 = ser.GetPackCategory();
                this.comboBox2.DataSource = list1.details;

                string ProductCategory = "";
                string PackCategory = "";

                if (!String.IsNullOrEmpty(ClsParmart.ProductCategory))
                {
                    this.comboBox1.SelectedItem = ClsParmart.ProductCategory;
                }
                if (!string.IsNullOrEmpty(ClsParmart.PackCategory))
                {
                    this.comboBox2.SelectedItem = ClsParmart.PackCategory;
                }



                if (this.comboBox1.SelectedValue != null)
                {
                    ProductCategory = this.comboBox1.SelectedValue.ToString();
                }
                if (this.comboBox2.SelectedValue != null)
                {
                    PackCategory = this.comboBox2.SelectedValue.ToString();
                }
                isInti = true;
                ////加载医院数据
                ProductListResult list2 = ser.GetProductList(null, null, null, null, ProductCategory, PackCategory);
                this.cboHospital.DisplayMember = "product_name";
                this.cboHospital.ValueMember = "product_id";
                this.cboHospital.DataSource = list2.details;
                if (!string.IsNullOrEmpty(ClsParmart.ProId))
                    this.cboHospital.SelectedValue = Convert.ToInt32(ClsParmart.ProId);


            }));
        }

        private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            insetLog("加载产品数据完毕！");
            button3.Enabled = true;
            timer1.Enabled = true;
        }

        private void Form1_Shown(object sender, EventArgs e)
        {

            insetLog("加载产品数据中....");
            button3.Enabled = false;
            backgroundWorker1.RunWorkerAsync();


        }

        object o = new object();
        private void 注册授权ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            PassWord passfrom = new PassWord();
            if (passfrom.ShowDialog() == DialogResult.OK)
            {
                Application.Run(new Form1());
            }
        }



        private void button7_Click(object sender, EventArgs e)
        {

            string mes1 = "12346578";
            Report report1 = new Report();
            report1.Load(@"print1.frx");
            // report1.SetParameterValue("par1", mes);
            report1.SetParameterValue("date", mes1);
            report1.PrintSettings.ShowDialog = false;
            report1.PrintSettings.Printer = ClsParmart.cboprint1;
            report1.Print();

        }

        private void 设置打印ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Form2 mb = new Form2();
            mb.Show();
        }



        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.Invoke(new Action(() =>
            {
                string ProductCategory = "";
                string PackCategory = "";

                if (this.comboBox1.SelectedValue != null)
                {
                    ProductCategory = this.comboBox1.SelectedValue.ToString();
                }
                if (this.comboBox2.SelectedValue != null)
                {
                    PackCategory = this.comboBox2.SelectedValue.ToString();
                }

                ////加载医院数据
                ProductListResult list2 = ser.GetProductList(null, null, null, null, ProductCategory, PackCategory);
                this.cboHospital.DisplayMember = "product_name";
                this.cboHospital.ValueMember = "product_id";
                this.cboHospital.DataSource = list2.details;

            }));
        }

        private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.Invoke(new Action(() =>
            {


                string ProductCategory = "";
                string PackCategory = "";

                if (this.comboBox1.SelectedValue != null)
                {
                    ProductCategory = this.comboBox1.SelectedValue.ToString();
                }
                if (this.comboBox2.SelectedValue != null)
                {
                    PackCategory = this.comboBox2.SelectedValue.ToString();
                }

                ////加载产品数据
                ProductListResult list2 = ser.GetProductList(null, null, null, null, ProductCategory, PackCategory);
                this.cboHospital.DisplayMember = "product_name";
                this.cboHospital.ValueMember = "product_id";
                this.cboHospital.DataSource = list2.details;

            }));
        }

        private void 大码设置ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Form5 mb = new Form5();
            mb.Show();
        }

        /// <summary>
        /// 将数字转换为26进制字母表示
        /// ClsParmart.PrintNumber转int后转换，超过26则重置为1
        /// </summary>
        /// <returns>26进制字母表示</returns>
        private string NumberToLetter()
        {
            int number;
            if (!int.TryParse(ClsParmart.PrintNumber, out number) || number < 1)
                number = 1;
            if (number > 26)
            {
                ClsParmart.PrintNumber = "1";
                number = 1;
            }
            char letter = (char)('A' + number - 1);

            return letter.ToString();
        }

        //不满大箱上传数据
        private void button1_Click(object sender, EventArgs e)
        {
            button1.Enabled = false;
            // 只有中箱满且大箱未满时允许上传
            if (centerCodeCount > 0 && centerCodeCount < boxSize && smallCodeCount == 0 && pendingMappings.Count > 0)
            {
                foreach (var item in pendingMappings)
                {
                    item.bigCode = "";

                }
              
                var ret = ser.BatchRelationLevel3(productId, 2, new List<BigCodeMapping>(pendingMappings));
             




                insetLog("手动上传时间：" + sj);
                if (!ret.success)
                {

                    insetLog("手动上传不满箱关联数据失败:" + ret.message);
                }
                else
                {

                    insetLog("手动上传不满箱关联数据成功");
                    // 清空内存和UI
                    pendingMappings.Clear();
                    centerCodes.Clear();
                    centerCodeCount = 0;
                    this.Invoke(new Action(() =>
                    {
                        this.listBox2.Items.Clear();
                        this.groupBox1.Text = "当前小码(" + smallCodeCount + ")";
                        this.groupBox4.Text = "当前中码(" + centerCodeCount + ")";
                        this.listBox3.Items.Clear();

                    }));
                    cboHospital.Enabled = true;
                    comboBox1.Enabled = true;
                    comboBox2.Enabled = true;
                }
            }
            else
            {
                insetLog("当前条件不满足：中箱必须满且大箱未满，且所有小码已采集完成。");
            }
            button1.Enabled = true;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (cboHospital.SelectedIndex != -1 && comboBox2.SelectedValue != null)
            {
                ProductItem nowtime = (ProductItem)cboHospital.SelectedItem;
                boxSize = nowtime.center_size;
                centerSize = nowtime.box_size;
                productId = nowtime.product_id;
                proName = nowtime.product_name + "(" + this.comboBox2.SelectedValue.ToString() + ")";

            }
            else
            {
                MessageBox.Show("请先选择产品");
                return;
            }
            if (this.textBox1.Text == "" || this.textBox3.Text == "")
            {
                MessageBox.Show("请输入号码和字母");
                return;
            }
            if (this.textBox3.Text.Length != 13)
            {
                MessageBox.Show("请输入正确的号码，13位!");
                return;
            }

            Report report1 = new Report();
            report1.Load(@"print1.frx");

            report1.SetParameterValue("date", this.textBox3.Text);
            report1.SetParameterValue("PrintNumber", this.textBox1.Text);
            report1.SetParameterValue("proName", proName);
            report1.PrintSettings.ShowDialog = false;
            report1.PrintSettings.Printer = ClsParmart.cboprint1;
            report1.Print();
        }

        private void button4_Click_1(object sender, EventArgs e)
        {
            if (cboHospital.SelectedIndex != -1 && comboBox2.SelectedValue != null)
            {
                ProductItem nowtime = (ProductItem)cboHospital.SelectedItem;
                boxSize = nowtime.center_size;
                centerSize = nowtime.box_size;
                productId = nowtime.product_id;
                proName = nowtime.product_name + "(" + this.comboBox2.SelectedValue.ToString() + ")";

            }
            else
            {
                MessageBox.Show("请先选择产品");
                return;
            }
            if (this.textBox1.Text == "" || this.textBox3.Text == "")
            {
                MessageBox.Show("请输入号码和字母");
                return;
            }
            if (this.textBox3.Text.Length != 13)
            {
                MessageBox.Show("请输入正确的号码，13位!");
                return;
            }
            Report report1 = new Report();
            report1.Load(@"print2.frx");
            report1.SetParameterValue("date", this.textBox3.Text);
            report1.SetParameterValue("PrintNumber", this.textBox1.Text);
            report1.SetParameterValue("proName", proName);
            report1.PrintSettings.ShowDialog = false;
            report1.PrintSettings.Printer = ClsParmart.cboprint1;
            report1.Print();
        }

        private void button5_Click(object sender, EventArgs e)
        {
            string ProductCategory = "";
            string PackCategory = "";
            string ProName = "";

            if (this.comboBox1.SelectedValue != null)
            {
                ProductCategory = this.comboBox1.SelectedValue.ToString();
            }
            if (this.comboBox2.SelectedValue != null)
            {
                PackCategory = this.comboBox2.SelectedValue.ToString();
            }
            if (!string.IsNullOrEmpty(this.textBox4.Text))
            {
                ProName = textBox4.Text.Trim();
            }


            ////加载产品数据
            ProductListResult list2 = ser.GetProductList(ProName, null, null, null, ProductCategory, PackCategory);
            this.cboHospital.DisplayMember = "product_name";
            this.cboHospital.ValueMember = "product_id";
            this.cboHospital.DataSource = list2.details;
        }

        private void button6_Click(object sender, EventArgs e)
        {
            string response = @"{
""name"": ""小明"",
""age"": 25
}";
            //var result = JsonConvert.DeserializeObject<List<BigCodeMapping>>(response);
            //response = "AAAAA";
            insetLog(response);

            // var result = JsonConvert.DeserializeObject<List<BigCodeMapping>>(response);
        }

        private void timer1_Tick(object sender, EventArgs e)
        {

            try { 
            DataTable tab = SqlDAL.GetRetryLog();
            if (tab!=null&&tab.Rows.Count > 0)
            {
                ExternalApiService ser1 = new ExternalApiService();

                var ret = ser1.BatchErrorDo(tab.Rows[0]["par_str"].ToString());
                if (!ret.success)
                {

                    SqlDAL.UpdateRetryFail(Convert.ToInt32(tab.Rows[0]["id"]));
                }
                else
                {
                    SqlDAL.UpdateRetrySuccess(Convert.ToInt32(tab.Rows[0]["id"]));
                }
              
            }
            }
            catch(Exception ee)
            {
                insetLog("定时任务出错"+ ee.Message);
            }
           // insetLog("定时上传");
        }
    }
}